package com.mycompany.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "booking")
public class Booking {
	@Embeddable
	public static class Id implements Serializable {		
		private static final long serialVersionUID = 1L;
		@Column(name = "USER_ID")
		private Long userId;
		@Column(name = "EVENT_ID")
		private Long eventId;
		
		public Id() {}
		
		public Id(Long userId, Long eventId) {
			this.userId = userId;
			this.eventId = eventId;
		}
		
		@Override
		public boolean equals(Object o) {
			if (o != null && o instanceof Id) {
				Id that = (Id)o;
				return this.userId.equals(that.userId) &&
				this.eventId.equals(that.eventId);
			} else {
				return false;
			}
		}
		@Override
		public int hashCode() {
			return userId.hashCode() + eventId.hashCode();
		}
	}
	
	@EmbeddedId
	private Id id = new Id();
	
	@Column(name = "FINISH_TIME")
	private double finishTime;
	
	@ManyToOne
	@JoinColumn(name="USER_ID",
			insertable = false,
			updatable = false)
	private User user;
	
	@ManyToOne
	@JoinColumn(name="EVENT_ID",
			insertable = false,
			updatable = false)
	private Event event;
	

	public Booking() {}
	
	public Booking(User user, Event event) {
		this.user = user;
		this.event = event;
		this.id.userId = user.getId();
		this.id.eventId = event.getId();
		
		user.getBookings().add(this);
		event.getBookings().add(this);
	}
	

	public Booking(Long userId, Long eventId) {
		this.id = new Id(userId, eventId);
	}

	@Column
	public double getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(double finishTime) {
		this.finishTime = finishTime;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public Id getId() {
		return id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Booking other = (Booking) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}

